$(function(){
	$('.buy_info>dl>dd').click(function(){
		if($(this).hasClass('selected') || $(this).hasClass('disabled'))return;
		$(this).parent().find('.selected').removeClass('selected');
		$(this).addClass('selected');
		var name = $(this).text();
		if(typeof(SPECS12[name]) !== 'undefined'){
			var specs = SPECS12[name];
			var check_spec = null;
			var dl = $(this).parent();
			if(dl.hasClass('spec1')){
				check_spec = dl.next();
			}else{
				check_spec = dl.prev();
			}
			if(check_spec.length){
				$(check_spec.find('dd')).each(function(){
					var spec_name = $(this).text();
					var has_exist = false;
					for(var i=0,c=specs.length;i<c;++i){
						if(specs[i] == spec_name){
							has_exist = true;
							break;
						}
					}
					if(has_exist){
						$(this).removeClass('disabled');
					}else{
						$(this).addClass('disabled');
					}
				});
			}
		}
		// $('#goods_info .buy_info').css({'border-color':'#E9E9E9'});
		check_spec_select();
	});

	$('#buy_btn').click(function(){
		var specs = '';
		$('#goods_info .buy_info>dl').each(function(){
			if($(this).find('>dd.selected').length)return;
			if(specs !== ''){
				specs += '、';
			}
			specs += '“' + $(this).attr('spec') + '”';
		});

		if(specs !== ''){
			//不可下单
			$('#selected_spec').addClass('red').find('label').text('请选择：');
			$('#selected_spec>span').text(specs);
			$('#goods_info .buy_info').css({'border-color':'red'});
			return;
		}

		//检查数量
		// if(!$('.buy_count').hasClass('error')){
			//有错误
			// var count = $('.buy_count').val();
			// $('#selected_spec>span').append('、“' + count + '”');
		// }
	});
});

function check_spec_select(){
	$('#selected_spec').removeClass('red').find('label').text('已选择：');
	var specs = '';
	var spec_name = '';
	$('#goods_info .buy_info>dl>dd.selected').each(function(){
		if(specs !== ''){
			specs += '、';
			spec_name += '_';
		}
		var sn = $(this).text();
		specs += '“' + sn + '”';
		spec_name += sn;
	});
	if(!$('.buy_count').hasClass('error')){
		//有错误
		var count = $('.buy_count').val();
		var unit = $('.unit').text();
		if(specs != ''){
			specs += '、';
		}
		specs += '“<span id="count_select">' + count + '</span> ' + unit + '”';

		if(typeof(SPEC_DATA[spec_name]) !== 'undefined'){
			var g = SPEC_DATA[spec_name];
			$('#price').text(g.price);
			$('input[name="goods_spec_id"]').val(g.spec_id);
			if(g.stock <= 0){
				$('#stock').text('（库存不足）');
			}else{
				$('#stock').text('（库存' + g.stock + unit + '）');
			}
		}
		$('.buy_info').removeAttr('style');
	}

	$('#selected_spec>span').html(specs);
}

function count_success(){
	// var count = $('.buy_count').val();
	// $('#count_select').text(count);
	$('#number_error_info').remove();
	check_spec_select();
}

function count_error(error_info){
	if(error_info === 'rule'){
		show_number_error('错误数量！');
	}else if(error_info.indexOf('min:' == 0)){
		var num = error_info.substr(4);
		show_number_error('数量不能小于：' + num);
	}else if(error_info.indexOf('max:' == 0)){
		var num = error_info.substr(4);
		show_number_error('数量不能大于：' + num);
	}
}

function show_number_error(error_info){
	if(!$('#number_error_info').length){
		$('.unit').after('<span id="number_error_info" class="errorMessage"></span>');
	}
	$('#number_error_info').html(error_info);
}